home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Mac OS X Throbber / Source / Utilities / UMiscUtils.cp < prev    next >
Encoding:
Text File  |  2000-06-24  |  4.3 KB  |  159 lines

  1. // ===========================================================================
  2. //    UMiscUtils.cp        ©1997-1999 Metrowerks Inc. All rights reserved.
  3. //    Original Author: John C. Daub
  4. // ===========================================================================
  5. //    A collection of various utilities.
  6.  
  7.  
  8. #include "UMiscUtils.h"
  9.  
  10. #include <LGrowZone.h>
  11.  
  12. #include <Traps.h>
  13. #include <Gestalt.h>
  14. #include <Patches.h>
  15. #include <Fonts.h>
  16.  
  17. bool StCriticalSection::sCriticalSection = false;
  18. bool StInterruptSection::sInterruptSection = false;
  19.  
  20.  
  21. // ---------------------------------------------------------------------------
  22. //    • System7Present                                [public]
  23. // ---------------------------------------------------------------------------
  24. //    This routine will determine if you're running at least System 7.0.0
  25. //    It will also check to make sure that the Gestalt trap is available
  26. //    (Gestalt came along with System 6.0.4).
  27.  
  28. bool
  29. UMiscUtils::System7Present()
  30. {
  31.     bool    has7 = false;
  32.     SInt32    sysVersion;
  33.     
  34.     if (UMiscUtils::TrapAvailable(_Gestalt) &&
  35.         (::Gestalt(gestaltSystemVersion, &sysVersion) == noErr) &&
  36.         (sysVersion >= 0x0700)) {
  37.             has7 = true;
  38.     }
  39.     
  40.     return has7;
  41. }
  42.  
  43.  
  44. // ---------------------------------------------------------------------------
  45. //    • TrapAvailable                                    [public]
  46. // ---------------------------------------------------------------------------
  47. //    Given a certain trap, see if it actually exists
  48.  
  49. bool
  50. UMiscUtils::TrapAvailable(
  51.     SInt16    inTrap)
  52. {
  53.     TrapType type = UMiscUtils::GetTrapType(inTrap);
  54.     
  55.     if (type == ToolTrap) {
  56.         inTrap &= 0x07FF;
  57.         
  58.         if (inTrap >= UMiscUtils::NumToolboxTraps()) {
  59.             inTrap = _Unimplemented;
  60.         }
  61.     }
  62.     
  63.     return (::NGetTrapAddress(static_cast<UInt16>(inTrap), type) != 
  64.                 ::NGetTrapAddress(_Unimplemented, ToolTrap));
  65. }
  66.  
  67.  
  68. // ---------------------------------------------------------------------------
  69. //    • GetTrapType                                    [public]
  70. // ---------------------------------------------------------------------------
  71. //    Given a trap, see if it's a toolbox trap or an OS trap (this
  72. //    information is stored in bit 11.  If set, toolbox, else OS)
  73.  
  74. TrapType
  75. UMiscUtils::GetTrapType(
  76.     SInt16    inTrap )
  77. {
  78.     return (((inTrap & 0x0800) > 0) ? ToolTrap : OSTrap);
  79. }
  80.  
  81.  
  82. // ---------------------------------------------------------------------------
  83. //    • NumToolboxTraps                                [public]
  84. // ---------------------------------------------------------------------------
  85. //    See how many traps we have
  86.  
  87. SInt16
  88. UMiscUtils::NumToolboxTraps()
  89. {
  90.     if (::NGetTrapAddress(_InitGraf, ToolTrap) == 
  91.             ::NGetTrapAddress(0xAA6E, ToolTrap)) {
  92.         return (0x0200);
  93.     } else {
  94.         return (0x0400);
  95.     }
  96. }
  97.  
  98.  
  99. // ---------------------------------------------------------------------------
  100. //    • MemoryIsLow                                    [public]
  101. // ---------------------------------------------------------------------------
  102. //    Determines if memory is low by checking to see if our growzone has
  103. //    fired off or not. Performs the added check of ensuring we even have a
  104. //    grow zone.
  105.  
  106. bool
  107. UMiscUtils::MemoryIsLow()
  108. {
  109.     PP_PowerPlant::LGrowZone* growZone = PP_PowerPlant::LGrowZone::GetGrowZone();
  110.     if (growZone == nil) {
  111.         return false;
  112.     }
  113.     
  114.         // Ask the grow zone if memory is low.
  115.     return growZone->MemoryIsLow();
  116. }
  117.  
  118.  
  119. // ---------------------------------------------------------------------------
  120. //    • IsFontInstalled                                [public]
  121. // ---------------------------------------------------------------------------
  122. // Given a particular font name to look for, make sure it's
  123. // installed.
  124.  
  125. bool
  126. UMiscUtils::IsFontInstalled(
  127.     ConstStr255Param    inFontName)
  128. {
  129.     bool    isInstalled = false; // Assume failure
  130.     
  131.         // Get the font number
  132.     SInt16    theFontNum;
  133.     ::GetFNum(inFontName, &theFontNum);
  134.     
  135.         // Chances are pretty good that if theFontNum is non-zero that
  136.         // it's installed, so short-circuit here and return as such
  137.     
  138.     if (theFontNum != 0) {
  139.         isInstalled = true;
  140.     } else {
  141.  
  142.             // The font is zero, that could mean it's not installed
  143.             // and could also mean that the font is the system font.
  144.             // Check for this by comparing the font strings.
  145.         Str255 sysFontName;
  146.         ::GetFontName(0, sysFontName);
  147.         
  148.             // To be safe, check for an empty string. If this happens
  149.             // to be the case, this could be a Bad Thing.
  150.         if (sysFontName[0] == 0) {
  151.             SignalStringLiteral_( "No system font found. This is probably bad..." );
  152.         }
  153.         
  154.         isInstalled = ::EqualString(inFontName, sysFontName, false, false);
  155.     }
  156.     
  157.     return isInstalled;
  158. }
  159.